import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import routes from "@/router/routes"

//进度条
import nProgress from 'nprogress'

import { get_Cookie } from '@/utils/setCookie'

import { check_login } from '@/api/login'

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//前置守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || "在线学习平台";

  nProgress.start();

  if (!to.meta.not_check) {
    if (!get_Cookie()) {
      next("/login");
      return;
    }

    //检查登录是否有效
    check_login({}).then((res) => {
     
      sessionStorage.setItem("user",JSON.stringify(res.data));
    })
  }

  //判断是否登录---cookie方法



  // axios.get("http://localhost:51725/api/login/Check_Login", {
  //   headers: {
  //     "Authorization": get_Cookie()
  //   }

  // }).then((res) => {
  //   if (res.data.code == 0) {
  //     console.log(res.data.data);

  //     sessionStorage.setItem("user",JSON.stringify(res.data.data));

  //   }
  // }).catch((res)=>{
  //   if(res.status===401){
  //     next("/login");
  //     return;
  //   }
  // })



  // }


  next();
})


//后置守卫
router.afterEach(() => {
  nProgress.done();
})


export default router

